import type { PQRequest } from '@prequest/types'
import type { ActionFn } from '../interceptorActionHandler'
import SingletonCancelToken from '../cancelToken'

/**
 * canceltoken处理器
 * @description 接口缓存
 * @param {object} request 请求上下文
 * @return {object}
 */
export const cancelTokenHandler: ActionFn = (request: PQRequest) => {
  const handler = () => {
    const cancelToken = new SingletonCancelToken()
    const source = cancelToken.getSource()
    request.cancelToken = source.token
    return request
  }

  return { checkHandle: true, value: handler }
}
